﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessComponents;
using DataAccessLayer;
using PresentationLayerMVC.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace PresentationLayerMVC.Controllers
{
    public class BookingController : Controller
    {
        BusinessComponentsFacade facade = new BusinessComponentsFacade();

        public ActionResult Index()
        {
            IEnumerable<Booking> bookings = facade.FindAllBooking();
            return View(bookings);
        }

        //
        // GET: /Booking/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Booking/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Booking/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Booking/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Booking/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Booking/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Booking/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }



        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ViewBooking()
        {
            BusinessComponentsFacade facade = new BusinessComponentsFacade();
            SearchModel sm = new SearchModel();
            sm.StartDate = DateTime.Now.Date;
            sm.EndDate = DateTime.Now.Date;

            //List<SearchModel.RoomTypeObject> objs = new List<SearchModel.RoomTypeObject>();


            //List<RoomType> rts = facade.findAllRoomType().ToList();
            //foreach (RoomType rt in rts)
            //{
            //    SearchModel.RoomTypeObject rto = new SearchModel.RoomTypeObject();
            //    rto.Id = rt;
            //    rto.Name = rt.RoomTypeName;
            //    objs.Add(rto);

            //}

            //sm.RoomTypeModels = objs;



            return View(sm);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ViewBooking(  SearchModel sm, String SearchButton)
        {
            //RoomType[] rts = sm.SelectedObjects;
            BusinessComponentsFacade facade = new BusinessComponentsFacade();
            sm.Result = facade.findBookingInRange(sm.StartDate, sm.EndDate,sm.GuestName);
            if (Request.IsAjaxRequest())
            {
                return View("SearchResult", sm.Result);
            }
            else
            {
                return View(sm);
            }
        }

        public class SearchModel
        {
            public DateTime StartDate { get; set; }

            public DateTime EndDate { get; set; }
            public String GuestName { get; set; }
            public IEnumerable<Booking> Result { get; set; }
           
            //public IList<RoomTypeObject> RoomTypeModels { get; set; }
            //public RoomType[] SelectedObjects { get; set; }

            //public class RoomTypeObject
            //{
            //    public RoomType Id { get; set; }
            //    public string Name { get; set; }
            //}

        }

     
        [HttpGet]
        public ActionResult CheckBooking(int bookingId)
        {
            BusinessComponentsFacade facade = new BusinessComponentsFacade();
            Booking booking = facade.findBooking(bookingId);
            BookingViewModels bookingviewmodel = new BookingViewModels();
            bookingviewmodel.booking = booking;
            bookingviewmodel.guests =facade.findBookingGuestbyBookingID(bookingId).ToList();
            Session["isBookingCancelable"] = facade.isBookingCancelable(bookingId);
            Session["isBookingUpdateable"] = facade.isBookingUpdateable(bookingId);
            return View(bookingviewmodel);
        }

        public ActionResult CheckBooking(BookingViewModels model)
        {

            return View(model);
        }


        public String EditGuest(BookingViewModels model)
        {
            for (int i = 0; i < model.guests.Count; i++)
            {
                GuestRepository guestRepo = new GuestRepository();
                Guest g = guestRepo.FindById(model.guests[i].ID);
                g.FirstName = model.guests[i].FirstName;
                g.LastName = model.guests[i].LastName;
                //UpdateModel(g);
                guestRepo.Commit();
            }

            return "Guest information save successfully";
        }

        public ActionResult Cancel(int id)
        {
            try
            {
                
                //Booking booking = facade.findBooking(id);
               
                BookingRepository bookingRepo = new BookingRepository();
                Booking booking = bookingRepo.FindById(id);
                booking.BookingStatus = "CANCEL";
                bookingRepo.Commit();

                BusinessComponentsFacade facade = new BusinessComponentsFacade();
                facade.SendCancelEmail(booking, booking.Guest, "http://loclhost:7777");
                return RedirectToRoute("http://localhost:7777/Check/"+ id);
            }
            catch
            {
                return View();
            }
        }


        public String EditUserInfo(BookingViewModels model)
        {
            BookingRepository bookingRepo = new BookingRepository();
            Booking booking = bookingRepo.FindById(model.booking.ID);
            booking.Guest.Address = model.booking.Guest.Address;
            booking.Guest.Phone = model.booking.Guest.Phone;
            booking.Guest.Email = model.booking.Guest.Email;
            booking.Guest.City = model.booking.Guest.City;
            booking.Guest.State = model.booking.Guest.State;
            booking.Guest.ZipPostalCode = model.booking.Guest.ZipPostalCode;
            bookingRepo.Commit();

            return "User information save successfully";
        }
     
      

    }
}